<template>
  <el-container class="home-container">
    <!-- 主页头部 -->
    <el-header>
      <div>
        <img src="" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" class="logOut" @click="logOut">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 主页左侧菜单 -->
      <el-aside :width="menuWidth">
        <button class="toggle-button" @click="toggleCollaps">||||</button>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#409EFF"
          :default-active="$route.path"
          unique-opened
          router
          :collapse="cutFold"
          :collapse-transition="false"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="auth.id + ''"
            v-for="auth in menuList"
            :key="auth.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i class="el-icon-location"></i>
              <!-- 一级菜单文本区 -->
              <span>{{ auth.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/home/' + item.path"
              v-for="item in auth.children"
              :key="item.id"
              :route="'/home/' + item.path"
            >
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 二级菜单图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单文本区 -->
                <span>
                  {{ item.authName }}
                </span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主页右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      cutFold: false,
    };
  },
  created() {
    this.getMenuList();
  },

  methods: {
    logOut() {
      sessionStorage.clear("token");
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http({
        method: "get",
        url: "/menus",
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    toggleCollaps() {
      this.cutFold = !this.cutFold;
    },
  },
  computed: {
    menuWidth() {
      return this.cutFold ? "66px" : "200px";
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #313743;
}
.el-main {
  background-color: #e9edf0;
}
.el-menu {
  border-right: solid 0px #e6e6e6;
}
.toggle-button {
  width: 100%;
  font-size: 10px;
  text-align: center;
  line-height: 24px;
}
</style>
